
var commonUtil = require("../../../utils/commonUtil.js")
var httpsUrl = getApp().globalData.httpsUrl;
var globalData = getApp().globalData;
Page({

  data: {
    imgUrl: globalData.imgUrl,
    user:[]
  },

  loadData: function (options) {
    //var user = wx.getStorageSync(globalData.userInfoKey);
    //this.setData({ user: user })
    let that = this;
    commonUtil.showLoading("加载中...");
    wx.request({
      header: { "Cookie": getApp().getJSESSIONID() },
      url: httpsUrl + "/member/info",
      success: function (res) {
        commonUtil.hideLoading();
        console.info("加载登录者信息:" + JSON.stringify(res));
        res = res.data;
        if (res.code == 0) {
          var user = res.data;
          if (user != null && user != '') {
            if (user.phone == 'empty') {
              user.phone = "";
            }
            wx.setStorageSync(globalData.userInfoKey, user);
          }
          that.setData({ user: user })
        } else if (res.code == 2) {
        } else {
          console.log("查询当前用户信息失败:" + res.msg);
        }
      }
    })
    
  },
  getPhoneNumber:function(e){
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
  },
  //保存
  saveUserInfo: function () {
    let user = this.data.user;
    let params = new Object();
    // params.phone = user.phone;
    params.name = user.name;
    //params.nickName = user.nickName;
    params.location = user.location;
    commonUtil.showLoading("稍等...");
    wx.request({
      header: { "Cookie": getApp().getJSESSIONID(), "content-Type": "application/x-www-form-urlencoded"},
      url: httpsUrl + "/member/updateInfo",
      dataType: "json",
      method: "POST",
      data: params,
      success: function (res) {
        commonUtil.hideLoading();
        let result = res.data;
        console.info("修改信息返回：" + JSON.stringify(res));
        if (result.code == 0) {
          wx.setStorageSync(globalData.userInfoKey, user);
          wx.switchTab({
            url: '../user/user',
          })
        } else {
          commonUtil.showModal("失败：" + result.msg);
        }
      }
    })
  },

  nameCg:function(e){
    let user = this.data.user;
    user.name = e.detail.value;
    this.setData({user:user})
  },
  nickNameCg: function (e) {
    let user = this.data.user;
    user.nickName = e.detail.value;
    this.setData({ user: user })
  },

  locationCg: function (e) {
    let user = this.data.user;
    user.location = e.detail.value;
    this.setData({ user: user })
  },
  phoneCg: function (e) {
    let user = this.data.user;
    user.phone = e.detail.value;
    this.setData({ user: user })
  },
  updatePhone:function(){
    wx.navigateTo({
      url: '../../login/updatePhone/updatePhone',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
    * 生命周期函数--监听页面加载
    */
  onLoad: function (options) {
    this.loadData();
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})